package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.query.ImageScanActivity;
import com.cmcc.linlaker.ui.query.ProjectDetailActivity;
import com.cmcc.linlaker.ui.query.bean.ProjectDetailBean;
import com.cmcc.linlaker.ui.upload.adapter.ImageAdapter;
import com.cmcc.linlaker.ui.upload.adapter.ImageAdapter2;
import com.cmcc.linlaker.ui.upload.bean.ImageBean;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.DensityUtil;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.view.BigImageDialog;
import com.cmcc.linlaker.view.GridSpacingItemDecoration;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UploadDetailActivity extends AppCompatActivity {
    private UploadQuestionBean.UploadQuestionData problemBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_detail);

        BarUtils.initStatusBar(this);

        problemBean = (UploadQuestionBean.UploadQuestionData) getIntent().getSerializableExtra("problemBean");

        initView();
    }

    private ImageAdapter2 adapter;
    private ArrayList<String> imageList = new ArrayList<>();
    private RecyclerView recycler_pic;


    private TextView text_title, text_code, text_address, text_date, text_person_name, text_project_name, text_pic_desc;

    public void initView() {
        recycler_pic = findViewById(R.id.recycler_pic);
        setRecyclerView(recycler_pic, imageList);

        text_title = findViewById(R.id.text_title);
        text_code = findViewById(R.id.text_code);
        text_address = findViewById(R.id.text_address);
        text_date = findViewById(R.id.text_date);
        text_person_name = findViewById(R.id.text_person_name);
        text_project_name = findViewById(R.id.text_project_name);
        text_pic_desc = findViewById(R.id.text_pic_desc);

        if (problemBean != null) {
            getProblemDetail();
        }
    }

    private void setRecyclerView(final RecyclerView recycler_view, final ArrayList<String> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new GridLayoutManager(this, 4));

        recycler_view.setNestedScrollingEnabled(false);
        recycler_view.addItemDecoration(new GridSpacingItemDecoration(4, DensityUtil.dip2px(this, 10), true));
        adapter = new ImageAdapter2(this, itemList);
        adapter.setOnItemClickListener(new ImageAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(UploadDetailActivity.this, ImageScanActivity.class);
                intent.putExtra("imageList", itemList);
                intent.putExtra("position", position);
                startActivity(intent);

//                BigImageDialog dialog = new BigImageDialog(UploadDetailActivity.this, R.style.dialog, itemList.get(position));
//                dialog.setCanceledOnTouchOutside(true);
//                dialog.show();
            }
        });
        recycler_view.setAdapter(adapter);
    }

    public void back(View view) {
        finish();
    }

    /**
     * 获取详情
     */
    private void getProblemDetail() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String url = ApiDefine.PROBLEM_DETAIL + problemBean.id;
        helper.sendGETRequest(url, UploadQuestionBean.UploadQuestionDetailData.class, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UploadQuestionBean.UploadQuestionDetailData bean = (UploadQuestionBean.UploadQuestionDetailData) data;
                    if (bean.code.equals("0")) {
                        UploadQuestionBean.UploadQuestionData detailData = bean.data;
                        text_title.setText(detailData.title);
                        text_code.setText(detailData.serialNumber);
                        text_address.setText(detailData.reportPlace);
                        text_date.setText(detailData.reportTime);
                        text_person_name.setText(detailData.createBy);
                        text_pic_desc.setText(detailData.description);

                        if (detailData.problemPhotos != null && detailData.problemPhotos.size() > 0) {
                            findViewById(R.id.text_no_photo).setVisibility(View.GONE);
                            recycler_pic.setVisibility(View.VISIBLE);
                            imageList.clear();
                            for (int i = 0; i < detailData.problemPhotos.size(); i++) {
                                imageList.add(detailData.problemPhotos.get(i).fastDfsServer + detailData.problemPhotos.get(i).groupName + "/" + detailData.problemPhotos.get(i).photoUrl);
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            findViewById(R.id.text_no_photo).setVisibility(View.VISIBLE);
                            recycler_pic.setVisibility(View.GONE);
                        }
                        if (!TextUtils.isEmpty(detailData.projectId)) {
                            findViewById(R.id.layout_project).setVisibility(View.VISIBLE);
                            getProjectDetail(detailData.projectId);
                        } else {
                            findViewById(R.id.layout_project).setVisibility(View.VISIBLE);
                            text_project_name.setText("无");
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(UploadDetailActivity.this);
                    }
                }
            }
        });
    }

    /**
     * 查询详情
     */
    private void getProjectDetail(String projectId) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String getUrl = ApiDefine.PROJECT_DETAIL + projectId;
        helper.sendGETRequest(getUrl, ProjectDetailBean.class, true, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    ProjectDetailBean bean = (ProjectDetailBean) data;
                    if (bean.code.equals("0")) {
                        text_project_name.setText(bean.data.name);
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(UploadDetailActivity.this);
                    } else {

                    }
                }
            }
        });
    }
}
